#ifndef _HTTP_LIB_H_
#define _HTTP_LIB_H_

#include <winsock2.h>
#include <stdio.h>

#define DEFAULT_PORT 80
#define DEFAULT_PROTO SOCK_STREAM
#define MAX_THREAD_NUM 10

typedef struct range{
	int start[MAX_THREAD_NUM];
	int end[MAX_THREAD_NUM];
}range;

typedef struct _MyData{
	char* server_name;
	char* fullpathname;
	char* storepathname;
	int port;
	int h_index;
}MYDATA,*PMYDATA;

/**
 *	connect to http server
 *	@param	server_name[in]		the server connected to
 *	@param	port[in]			the port number, if 0 is given, DEFAULT_PORT 80 will be used by replacement
 *	@param	connect_socket[out]	the connected SOCKET via which data will be transferred
 *	@return 0 for OK
 */
int Connect(const char* server_name, int port, SOCKET* connect_socket);

/**
 *	download the file via the HTTP protocol
 *	@param	lp[in]		PMYDATA structure, including all the parameters
 *	@return 0 for OK
 */
int HttpGet(void* lp);

/**
 *	parse the response from the server after sending the "GET" request
 *	@param	response[in]		response from the server
 *	@param	recv_len[in]		the length of the response
 *	@param	conn_socket[in]		the SOCKET that Connect() function returned
 *	@param	filename[in]		the name of file that is to be downloaded and saved
 *	@param	fHandle[in]			the HANDLE of file that is to be downloaded and saved
 *	@param	pData[in]			the PMYDATA structure, including all the parameters each thread needing
 *	@return 0 for OK
 *			-1 for Error
 */
int HttpParse(char* response, int recv_len, SOCKET conn_socket, const char* filename, HANDLE fHandle, PMYDATA pData);


/**
 *	save the data into the hardware(local machine)
 *	@param	filename[in]		the name of file that is to be written
 *	@param	cont_len[in]		the size of file that is to be written
 *	@param	body_to_write[in]	the length of the very first part of the body that is received following the header
 *	@param	conn_socket[in]		the SOCKET that Connect() function returned
 *	@param	response[in]		response from the server
 *	@param	fHandle[in]			the HANDLE of file that is to be downloaded and saved
 *	@param	pData[in]			the PMYDATA structure, including all the parameters each thread needing
 *	@return 0 for OK
 *			-1 for Error
 */
int WriteToFile(const char* filename, int file_size, int body_to_write, SOCKET conn_socket, char* response, HANDLE fHandle, PMYDATA pData);

/**
 *	the wrapper of the function HttpGet, used in multithread mode
 *	@param	server_name[in]		the server connected to
 *	@param	port[in]			the port number, if 0 is given, DEFAULT_PORT 80 will be used by replacement
 *	@param	fullpathname[in]	the name of file together with the path, such as "/target.xxx" or "/xxx/target.xxx"
 *	@param	thread_num[in]		the number of threads via which download the file
 *	@param	storepath[in]		the location where the file would be put in
 *	@return	0 for OK
 */
int HttpDownload(char* server_name, int port, char* fullpathname, int thread_num, char* storepathname);
#endif //_HTTP_LIB_H_